package cn.fintecher.pangolin.service.business.scheduled;

import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.mysql.CaseInfo;
import cn.fintecher.pangolin.entity.mysql.QCaseInfo;
import cn.fintecher.pangolin.service.business.model.RetireCaseModel;
import cn.fintecher.pangolin.service.business.repository.elasticsearch.BaseCaseRespository;
import cn.fintecher.pangolin.service.business.repository.elasticsearch.HistoryCasesRespository;
import cn.fintecher.pangolin.service.business.repository.jparepository.AssistCaseRepository;
import cn.fintecher.pangolin.service.business.repository.jparepository.CaseInfoRepository;
import cn.fintecher.pangolin.service.business.repository.jparepository.ProcessApplicationRepository;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * @Author wangxiangdong
 * @Description 已分配案件到期执行自动退案操作
 * @Date 2019/10/29 14:45
 **/
@Component
@EnableScheduling
@Slf4j
public class RetireCaseScheduled {

    @Autowired
    private CaseInfoRepository caseInfoRepository;

    @Autowired
    private HistoryCasesRespository historyCasesRespository;

    @Autowired
    private RetireCaseHandler retireCaseHandler;

    @Autowired
    private BaseCaseRespository baseCaseRespository;

    @Autowired
    private AssistCaseRepository assistCaseRepository;

    @Autowired
    private ProcessApplicationRepository processApplicationRepository;

    @Scheduled(cron = "0 30 0 * * ?")
    void retireCase() throws ExecutionException, InterruptedException {
        List<RetireCaseModel> retireCaseModelArrayList = new ArrayList<>();
        log.info("已分配案件系统自动退案调度任务开始:{}", ZWDateUtil.getFormatNowDateTime("yyyy-MM-dd HH:mm:ss"));
        List<CaseInfo> caseInfoList = Lists.newArrayList(caseInfoRepository.findAll(QCaseInfo.caseInfo.endCaseDate.before(ZWDateUtil.getNowDate())));
        if (caseInfoList.size() == 0) {
            log.info("已分配案件系统自动退案调度任务结束，没有退案的案件:{}", ZWDateUtil.getFormatNowDateTime("yyyy-MM-dd HH:mm:ss"));
            return;
        }
        List<List<CaseInfo>> partCaseInfo = Lists.partition(caseInfoList, 5000);
        for (List<CaseInfo> part : partCaseInfo) {
            CompletableFuture<RetireCaseModel> retireCaseModelCompletableFuture = retireCaseHandler.retireCaseHandler(part);
            retireCaseModelArrayList.add(retireCaseModelCompletableFuture.get());
        }
        log.info("执行结果收集完成 {}，共有:{}个结果", ZWDateUtil.getFormatNowDateTime("yyyy-MM-dd HH:mm:ss"),retireCaseModelArrayList.size());
        for(int i =0;i< retireCaseModelArrayList.size();i++){
            log.info("正在处理第{}个结果集",i);
            RetireCaseModel retireCaseModel = retireCaseModelArrayList.get(i);
            historyCasesRespository.saveAll(retireCaseModel.getHistoryCasesList());
            assistCaseRepository.saveAll(retireCaseModel.getAssistCaseList());
            processApplicationRepository.saveAll(retireCaseModel.getProcessApplicationList());
            baseCaseRespository.deleteAll(retireCaseModel.getBaseCaseList());
        }
        //删除CaseInfo
        if (caseInfoList.size() <= 10000) {
            caseInfoRepository.deleteAll(caseInfoList);
        } else {
            int flag = (int) Math.ceil((double) caseInfoList.size() / 10000);
            for (int i = 0; i < flag; i++) {
                Pageable pageable = PageRequest.of(i, 10000);
                List<CaseInfo> caseInfos = caseInfoList.stream().skip(pageable.getPageNumber() * pageable.getPageSize()).limit(pageable.getPageSize()).collect(Collectors.toList());
                caseInfoRepository.deleteAll(caseInfos);
            }
        }
        log.info("已分配案件系统自动退案调度任务结束:{}", ZWDateUtil.getFormatNowDateTime("yyyy-MM-dd HH:mm:ss"));
    }

}
